﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using LogHelp;

namespace TimeUpdate
{
    public partial class Form1 : Form
    {
        private bool isTimeToUpdateLineOne = false;
        private bool isTimeToUpdateLineTwo = false;
        private int countOne = 1;
        private int countTwo = 1;
        string oldDay = DateTime.Now.ToString("yyyy-MM-dd");
        Log log = new Log();
        private int lineOne = Convert.ToInt32(ConfigurationManager.AppSettings["lineOne"]);
        private int lineTwo = Convert.ToInt32(ConfigurationManager.AppSettings["lineTwo"]);
        public Form1()
        {
            InitializeComponent();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                TimeUpdate.Access.TimeUpdatAccess _teamAccessOne = new Access.TimeUpdatAccess(lineOne);
                TimeUpdate.Access.TimeUpdatAccess _teamAccessTwo = new Access.TimeUpdatAccess(lineTwo);
                //string hour = DateTime.Now.Hour.ToString();
                //string minute = DateTime.Now.Minute.ToString();
                //string setHour = this.cmb_Hour.Text.ToString();
                //string setMinute = this.cmb_Minute.Text.ToString();
                //string newDay = DateTime.Now.ToString("yyyy-MM-dd");

                int hour = Convert.ToInt32(DateTime.Now.Hour.ToString());
                int minute = Convert.ToInt32(DateTime.Now.Minute.ToString());
                int setHour = Convert.ToInt32(this.cmb_Hour.Text.ToString());
                int setMinute = Convert.ToInt32(this.cmb_Minute.Text.ToString());
                string newDay = DateTime.Now.ToString("yyyy-MM-dd");


                if (newDay != oldDay)
                {
                    countOne = 1;
                    countTwo = 1;
                    oldDay = newDay;
                }
                if (hour == setHour && minute == setMinute)
                {
                    isTimeToUpdateLineOne = true;
                    isTimeToUpdateLineTwo = true;

                }

                //更新线路1
                if (isTimeToUpdateLineOne && countOne == 1 && !_teamAccessOne.IsAlreadyUpdate())
                {

                    //增加今日将交路交给司机
                    _teamAccessOne.AddRecordToRefreshCrossRoad();
                    //写入出勤信息表
                    _teamAccessOne.AddAttenRecord();
                    //班组轮换
                    _teamAccessOne.UpdateClassPlane();
                    //写入每日班组轮换记录
                    _teamAccessOne.AddTrunGroupsRecord();

                    //删除临时出勤信息
                    _teamAccessOne.DeleteTempAttend();
                    //更改DriverGroup的司机信息
                    _teamAccessOne.UpdateDriverInfo();
                    
                    countOne++;
                }
                //更新线路2
                if (isTimeToUpdateLineTwo && countTwo == 1 && !_teamAccessTwo.IsAlreadyUpdate())
                {

                    //增加今日将交路交给司机
                    _teamAccessTwo.AddRecordToRefreshCrossRoad();
                    //写入出勤信息表
                    _teamAccessTwo.AddAttenRecord();
                    //班组轮换
                    _teamAccessTwo.UpdateClassPlane();
                    //写入每日班组轮换记录
                    _teamAccessTwo.AddTrunGroupsRecord();
                    //删除临时出勤信息
                    _teamAccessTwo.DeleteTempAttend();
                    //更改DriverGroup的司机信息
                    _teamAccessTwo.UpdateDriverInfo();
                    
                    countTwo++;
                }
                isTimeToUpdateLineOne = false;
                isTimeToUpdateLineTwo = false;

            }
            catch (Exception ex)
            {
                log.writeError(ex.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.cmb_Hour.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["Hours"]); ;
            this.cmb_Minute.Text = ConfigurationManager.AppSettings["Minute"];
        

        }


        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            this.Hide();
            this.ShowInTaskbar = false;
            this.notifyIcon1.Visible = true;


        }

        private void btn_Sure_Click(object sender, EventArgs e)
        {
            try
            {
                bool flag = false;
                int hour = Convert.ToInt32(this.cmb_Hour.Text.Trim().ToString());
                int minute = Convert.ToInt32(this.cmb_Minute.Text.Trim().ToString());
                if (hour < 0 || hour > 23 || minute < 0 || minute > 59)
                {
                    MessageBox.Show("小时只能输入0-23之间的数字，分钟只能输入0－59之间的数字");
                    flag = false;
                }
                else
                {
                    flag = true;
                }

                if (flag)
                {
                    this.timer1.Enabled = true;
                    MessageBox.Show("设置保存成功！");

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("请输入合法的数值！");
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            this.ShowInTaskbar = false;
            this.notifyIcon1.Visible = true;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this.Close();
            this.Dispose();
            Application.Exit();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.Show();
            this.ShowInTaskbar = true;
            this.WindowState = System.Windows.Forms.FormWindowState.Normal;
            this.notifyIcon1.Visible = true;
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            TimeUpdate.Access.TimeUpdatAccess _teamAccess = new Access.TimeUpdatAccess();

            if (_teamAccess.TestConnection())
            {
                MessageBox.Show("数据库测试连接成功！");
            }
            else
            {
                MessageBox.Show("数据库测试连接失败！");
            }
        }
    }



}
